<?php
use App\Models\Area;
use Illuminate\Http\Request;

/***
 * 获得指定城市信息
 * @param string $citycode
 * @return array
 */
function GetCitys($citycode = "")
{

    $citys = [];

    $city = Area::all();
    //需要整理两次才能得到最终结果
    foreach ($city as $k => $v) {
        if ($v->leveltype == 0) {
            $citys[$v->code]['province'] = $v->toArray();
        }
        if ($v->leveltype == 1) {
            $citys[$v->supcode]['city'][$v->code] = $v->toArray();
        }
        if ($v->leveltype == 2) {
            $region[$v->supcode][] = $v->toArray();
        }
    }

    foreach ($citys as $key => $value) {
        foreach ($value['city'] as $k => $v) {
            if (isset($region[$k])) {
                $citys[$key]['city'][$k]['region'] = $region[$k];
            }
        }
    }

    if (!empty($citycode)) {
        $city_sign = [];
        foreach ($citys as $key => $value) {
            foreach ($value['city'] as $k => $v) {
                if ($k == $citycode) {
                    $city_sign[$key]['province'] = $citys[$key]['province'];
                    $city_sign[$key]['city'][$k] = $citys[$key]['city'][$k];
                    break 2;
                }
            }
        }
        $citys = $city_sign;
    }

    return $citys;
}

/**
 * 获得订单号
 * Created by tommy on 2023/10/30
 * @param $prefix  JJ/HK : 捷洁/嗨卡
 * @return string;
 *
 */
function OrderSN($prefix = 'JJ')
{
    $order_sn = $prefix.date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
    return $order_sn;
}

//开启curl post请求
function SendPostCmd($url,$data)
{
    $curl = curl_init(); // 启动一个CURL会话
    curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检测
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:')); //解决数据包大不能提交
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
    curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循
    curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
    $tmpInfo = curl_exec($curl); // 执行操作
    if (curl_errno($curl)) {
        echo 'Errno'.curl_error($curl);
    }
    curl_close($curl); // 关键CURL会话
    return $tmpInfo; // 返回数据
}


function jsonReturn($data = [], $code = 0, $message = 'success')
{
    return response()->json([
        'code' => $code,
        'data' => $data,
        'msg' => $message
    ]);
}


//转换数组下票，以数组指定的值作为下标
function ArrayReplaceIndex($arra , $keyname)
{
    $res = [];
    foreach ($arra as $key => $value) 
    {
        $res[$value[$keyname]][] = $value;
    }
    return $res;
}

/**
 * 下面的脚本是和德意app做数据交换的时候会用到，目前用到的场景：
 * 1.当用户有洗车消费成功后，会异步推送一条数据给德意的接口。
 * 
 */ 
Trait Deyi {
    
     //解密
    public function decrypt($data, $key)
    {
        return openssl_decrypt(base64_decode(hex2bin($data)), 'DES-ECB', $key, OPENSSL_RAW_DATA);
    }

    //加密
    public function encrypt($data, $key)
    {
        return bin2hex(base64_encode(openssl_encrypt($data, 'DES-ECB', $key, OPENSSL_RAW_DATA)));
    }
}

?>
